package com.xiansi.controller;

import com.github.pagehelper.PageInfo;
import com.xiansi.pojo.Game;
import com.xiansi.pojo.Player;
import com.xiansi.service.PlayerService;
import com.xiansi.vo.QueryPlayerVO;
import com.xiansi.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("player")
@ResponseBody
public class PlayerController {
    @Resource
    private PlayerService playerService;

    /**
     * 分页查询信息
     *
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Player> queryByPage(Integer pageNum, Integer pageSize, QueryPlayerVO vo) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 1;
        }
        PageInfo<Player> playerPageInfo = playerService.queryByPageInfo(pageNum, pageSize, vo);
        return new ResultVO<>(playerPageInfo);
    }

    /**
     * 新增球员
     *
     * @param player
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Player> addPlayer(Player player) {
        Integer i = playerService.addPlayer(player);
        if (i == 1) {
            return new ResultVO<Player>();
        }
        return new ResultVO<Player>(500, "请稍后再试");
    }

    /**
     * 根据id查询球员信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Player> queryById(@PathVariable("id") Integer id) {
        Player player = playerService.queryById(id);
        return new ResultVO<Player>(player);
    }

    /**
     * 根据id修改信息
     *
     * @param id
     * @param player
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<Player> update(@PathVariable("id") Integer id, Player player) {
        player.setPlayerId(id);
        Integer i = playerService.updatePlayer(player);
        if (i == 1) {
            return new ResultVO<Player>();
        }
        return new ResultVO<Player>(500, "请稍后再试");

    }

    /**
     * 根据id删除球员信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Player> delete(@PathVariable("id") Integer id) {
        Integer i = playerService.deletePlayer(id);
        if (i == 1) {
            return new ResultVO<Player>();
        }
        return new ResultVO<Player>(500, "请稍后再试");
    }


}
